<template>
  <div class="page-container">
    <!-- 面包屑部分 -->
    <div class="breadcrumb">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item>会员中心</el-breadcrumb-item>
        <el-breadcrumb-item>会员等级管理</el-breadcrumb-item>
        <el-breadcrumb-item>等级权益设置</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!--工具栏-->
    <div class="toolbar">
      <el-form :inline="true" :model="filters" :size="size" ref="filters" >
        <el-row class="search-input">
          <el-form-item style="margin-bottom:0 !important">
            <el-form-item label="创建时间：" prop="startTime" style="margin-right:0 !important">
              <el-date-picker
                v-model="filters.startTime"
                type="datetime"
                placeholder="开始时间"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                clearable
                @change="changeStartDate"
                :picker-options="startDateDisabled"
              ></el-date-picker>
            </el-form-item>
            <span class="line">-</span>
            <el-form-item prop="endTime">
              <el-date-picker
                v-model="filters.endTime"
                type="datetime"
                placeholder="结束时间"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                clearable
                @change="changeEndDate"
                :picker-options="endDateDisabled"
              ></el-date-picker>
            </el-form-item>
            </el-form-item>
          <el-form-item label="等级名称：">
            <el-input v-model="filters.levelName" placeholder="输入等级名称" maxlength="80" clearable></el-input>
          </el-form-item>
        </el-row>
        <el-row class="search-button-noborder" style="text-align: right">
          <el-form-item>
            <kt-button icon="el-icon-search" :label="'搜索'" type="primary" @click="findPageClick(null)" :size="size"/>
          </el-form-item>
          <el-form-item>
            <kt-button :label="$t('action.reset')" type="primary" plain @click="resetForm('filters')" :size="size"/>
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <div class="toolbar" >
      <table-column-filter-dialog
        ref="tableColumnFilterDialog"
        :columns="columns"
        @handleFilterColumns="handleFilterColumns"
      ></table-column-filter-dialog>
    </div>
    <!--表格内容栏-->
      <el-table
        :data="tableList"
        :header-cell-style="{ 'color': '#FFF','background-color': '#409EFF','text-align': 'center'}"
        :highlight-current-row="true"
        :max-height="15000"
        :size="'mini'"
        style="width: 100%"
        class="specialTable"
        :border="false"
        v-loading="loading"
      >
        <el-table-column align="center" label="编号"  element-loading-text>
          <template slot-scope="scope">
            <span>{{ scope.row.id}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="等级名称"  element-loading-text>
          <template slot-scope="scope">
            <span>{{ scope.row.levelName || '-'}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="所属组织"  element-loading-text>
          <template slot-scope="scope">
            <span>{{ scope.row.deptName || '-'}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="权益数"  element-loading-text>
          <template slot-scope="scope">
            <span>{{ scope.row.roleTotal || '-'}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="创建时间"  element-loading-text>
          <template slot-scope="scope">
            <span>{{ scope.row.createTime || '-'}}</span>
          </template>
        </el-table-column>
        
        <el-table-column class-name="status-col" align="left" fixed="right" label="操作" >
          <template slot-scope="scope">
            <kt-button
              @click="handleConfiguration(scope.row)" type="text" size="mini" style label="配置权益" >
            </kt-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页栏-->
      <div class="toolbar" style="padding:10px;">
        <el-pagination
          layout="total, sizes, prev, pager, next, jumper"
          @current-change="currentChange"
          :current-page="filters.start"
          :page-size="filters.limit"
          :total="totalSize"
          style="float:right;"
          :page-sizes="[10, 20, 30, 40]"
          @size-change="sizeChange"
        ></el-pagination>
      </div>
  </div>
</template>

<script>
  import {exportExcel} from "@/utils/excel";
  import CyTable from "@/views/Core/JicTable";
  import KtButton from "@/views/Core/KtButton";
  import {format} from "@/utils/datetime";
  import {validatePhone,validateContacts,isEmail,isNumber} from '@/utils/validate'
  import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";
  import XLSX from "xlsx";
  export default {
    components: {
      CyTable,
      KtButton,
      TableColumnFilterDialog
    },
    data() { 
      return {
        startDateDisabled: {},//开始时间限制
        endDateDisabled: {},//结束时间限制
        totalSize:0,
        tableList:[],
        loading:false,
        isMom:'',
        childId:'',
        data:[],
        deptType:JSON.parse(localStorage.getItem('user')).deptType,
        size: "mini",
        filters: { 
          startTime:null,   
          endTime:null,
          levelName:null,
          childId:null,   
        },
        columns:[
          {prop: "id", label: "编号", minWidth: 90},
          {prop: "levelName", label: "等级名称", minWidth: 120},
          {prop: "deptName", label: "所属组织", minWidth: 120},
          {prop: "deptType", label: "组织层级", minWidth: 120,formatter: this.typeFormatter},
          {prop: "roleTotal", label: "权益数", minWidth: 120},
          {prop: "createTime", label: "创建时间", minWidth: 120}, 
          ],
        filterColumns: [],
        pageRequest: {pageNum: 1, pageSize: 10},
      };
    },
    methods: {
      changeStartDate: function() {
        var this_ = this;
        this_.endDateDisabled = {
          disabledDate: time => {
            if (this_.filters.startTime) {
              return time.getTime() <= new Date(this_.filters.startTime).getTime();
            }
          }
        };
      },
      changeEndDate: function() {
        var this_ = this;
        this_.startDateDisabled = {
          disabledDate: time => {
            if (this_.filters.endTime) {
              return time.getTime() >= new Date(this_.filters.endTime).getTime();
            }
          }
        };
      },
      // 换页刷新
      currentChange(pageNum) {
        this.pageRequest.pageNum = pageNum;
        this.findPage();
      },
      sizeChange(pageSize) {
        this.pageRequest.pageNum = 1;
        this.pageRequest.pageSize = pageSize;
        this.findPage();
      },
      //查询权益
      showLookConfiguration(data){
        this.$router.push({path: "/PowerLookVipTemplate",query: { id: data.id, type: 1 }});
      },
      //组织层级解析
      typeFormatter: function(row, column, cellValue, index){
        if(row.deptType==0){
          return '母平台'
        }else if(row.deptType==1){
          return '子平台'
        }else if(row.deptType==2){
          return '商家'
        }else if(row.deptType==3){
          return '店铺'
        }
      },
      //配置权限弹框
      handleConfiguration(data){
        this.$router.push({path:"/PowerTemplate" , query:{"id":data.id}});
      },
       //点击搜索，页码重置
      findPageClick(){
        this.pageRequest.pageNum = 1;
        this.findPage();
      },
      // 获取分页数据
      findPage: function(data) {
        if(data != null && data.start != null){
        if(data.start != null){
          this.pageRequest.pageNum = data.start;
        }
        if(data.limit != null){
          this.pageRequest.pageSize = data.limit;
        }
       }
        let this_ = this;
        if (this_.filters == undefined || this_.filters == null) {
          this_.filters = {};
        }
        this_.filters.start = this.pageRequest.pageNum;
        this_.filters.limit = this.pageRequest.pageSize;
        this.utils.request.queryMegradeRuleList(this_.filters, function (res) {
          if (res.data == null) {
              res.data = [];
              this_.$refs.CyTable.findPage({content:res.data.rows,total:res.data.total});
              res.totle = 0;
          }else{
            if(res.data.rows.length>0){
              this_.childId=res.data.rows[0].deptId;
              this_.isMom=res.data.rows[0].isMom;
            }
              this_.tableList=res.data.rows;
              this_.totalSize=res.data.total;
          }
        });
      },

      //重置方法
      resetForm(formName) {
        //需要校验的key
      let keys = ["startTime","endTime","levelName","childId","merchantOrStoreName"]
      let boo = this.seachTableUtils.checkFromIsNull(this.filters,keys)
      if(!boo){
        return
      }
      if (this.$refs[formName] != undefined) {
          this.$refs[formName].resetFields();
          this.filters={
            startTime:null,   
            endTime:null,
            levelName:null,
            childId:null, 
          };
      }
      this.findPage();
      },

      // 处理表格列过滤显示
      displayFilterColumnsDialog: function () {
        this.$refs.tableColumnFilterDialog.setDialogVisible(true);
      },
      // 处理表格列过滤显示
      handleFilterColumns: function (data) {
        this.filterColumns = data.filterColumns;
        this.$refs.tableColumnFilterDialog.setDialogVisible(false);
      },
      // 处理表格列过滤显示
      initColumns: function () {
        var temp = [];
        $.each(this.columns, function (key, val) {
          temp.push(val);
        });
        this.filterColumns = temp;
      },
    },
    mounted() {
      this.findPage();
    }
  };
</script>

<style scoped lang='scss'>
/deep/ .el-transfer-panel{
   width: 250px !important;
}
.search-input{
  .times {
    .times-to {
      padding: 0 10px;
      height: 32px;
      line-height: 32px;
      color: #999;
      display: inline-block;
    }
    /deep/ .el-input{
      width:auto !important;
      /deep/ .el-input__inner{
        width: auto !important;
      }
    }
  }
}
</style>
